package com.weidian.mobile.open.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.weidian.mobile.open.Message;
import com.weidian.mobile.open.pojo.SaleMachine;
import com.weidian.mobile.open.pojo.SaleWechatFriend;
import com.weidian.mobile.open.pojo.SaleWechatMsg;
import com.weidian.mobile.open.pojo.User;
import com.weidian.mobile.open.repository.SaleMachineRepository;
import com.weidian.mobile.open.repository.SaleWechatFriendRepository;
import com.weidian.mobile.open.repository.SaleWehcatMagRepository;
import com.weidian.mobile.open.repository.UserRepository;
import com.weidian.mobile.open.utils.HttpUtils;
import com.weidian.mobile.open.utils.StringUtils;


@Controller
@RequestMapping(value="/phone")
public class MobileWechatController {
	
	@Autowired
	UserRepository userRepository;
	
	@Autowired
	SaleMachineRepository saleMachineRepository;
	
	@Autowired
	SaleWechatFriendRepository saleWechatFriendRepository;
	
	@Autowired
	SaleWehcatMagRepository saleWehcatMagRepository;
	
	@Autowired
	StringRedisTemplate jedisTemplate;
	 
	
	//-------------------------------------------------消息-----------------------------
		
	@RequestMapping(value="/add_msg", method=RequestMethod.POST)
	@ResponseBody
	public Object addChatMsg(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		String talker = req.getParameter("talker");
		String content = req.getParameter("content");
		String IMID = req.getParameter("IMID");
		//消息存入数据路
		
		
		return "";
	}
	
	public static void main(String[] args) throws Exception {
		JSONObject jsonObject1 = new JSONObject();
		jsonObject1.put("type", "txt");
		jsonObject1.put("msg", StringUtils.encode("123"));
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("target_type", "users");
		jsonObject.put("target", new String[]{"f65ddb2d1e307b7e"});
		jsonObject.put("msg", jsonObject1);
		String ret = HttpUtils.sendPostforEasemob("https://a1.easemob.com/1119161217115396/wonsionton/" + "Strings", jsonObject.toJSONString(), "Bearer YWMtd8lSdmC6EeiFGBV2i7gOBQAAAAAAAAAAAAAAAAAAAAHmBS4A_CgR55N1q1X5Mk69AgMAAAFjm3QGwgBPGgApRFB-SsECHXbbv5vnp7V-kxhcGM75cwFTQYrf9Y6-tg");
		System.err.println(ret);
	}
	
	@RequestMapping(value="/send_msg", method=RequestMethod.GET)
	@ResponseBody
	public Object sendChatMsg(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		String token = jedisTemplate.opsForValue().get("esmobtoken");
		JSONObject jsonObject1 = new JSONObject();
		jsonObject1.put("type", "txt");
		jsonObject1.put("msg", StringUtils.encode("123"));
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("target_type", "users");
		jsonObject.put("target", new String[]{"f65ddb2d1e307b7e"});
		jsonObject.put("msg", jsonObject1);
		String ret = HttpUtils.sendPostforEasemob("https://a1.easemob.com/1119161217115396/wonsionton/" + "Strings", jsonObject.toJSONString(), "Bearer " + token);
		if (ret.contains("error")) {
			return ret;
		}
		return "";
	}
	
	 @RequestMapping(value="/delnotice",method=RequestMethod.POST)
	 @ResponseBody
	 public Object chatContentSave(HttpServletRequest req, HttpServletResponse resp){
		 String imei = req.getParameter("imei"); 
		 String uin = req.getParameter("uin"); 
		 String userId = req.getParameter("userId");
    	 Integer UserId = new Integer(userId);
    	 return saleMachineRepository.findByUserIdAndMeid(UserId, imei);
	 }
	
	@RequestMapping(value="/save_msg", method=RequestMethod.POST)
	@ResponseBody
    public Object addWechatFriendMsg(HttpServletRequest req, HttpServletResponse resp) {
		try {
			String userId = req.getParameter("userId");
	   	 	Integer UserId = new Integer(userId);
			String wechatId = req.getParameter("wechatId");
	    	String wechatFriendId = req.getParameter("wechatFriendId");
	    	String msgContent = req.getParameter("msgContent");
	    	String ret = req.getParameter("flag");
	    	Integer flag = new Integer(ret);
	    	msgContent = StringUtils.decode(msgContent);
	    	System.err.println(msgContent);
	    	String sendTime = req.getParameter("sendTime");
	    	SaleWechatMsg saleWechatMsg = new SaleWechatMsg();
	    	saleWechatMsg.setUserId(UserId);
	    	saleWechatMsg.setWechatId(wechatId);
	    	saleWechatMsg.setFriendWechatId(wechatFriendId);
	    	saleWechatMsg.setContent(msgContent);
	    	saleWechatMsg.setCreateTime(System.currentTimeMillis());
	    	saleWechatMsg.setSendTime(sendTime);
	    	saleWechatMsg.setFlag(flag);
	    	return Message.success(saleWehcatMagRepository.save(saleWechatMsg));
		} catch (Exception e) {
			return Message.error();
		}
	}
	
	@RequestMapping(value="/save_fri", method=RequestMethod.POST)
	@ResponseBody
    public Object addWechatFriend(HttpServletRequest req, HttpServletResponse resp) {
		try {
			String userId = req.getParameter("userId");
	   	 	Integer UserId = new Integer(userId);
			String wechatId = req.getParameter("wechatId");
	    	String wechatFriendId = req.getParameter("wechatFriendId");
	    	String wechatFriendName = req.getParameter("wechatFriendName");
	    	String ret = req.getParameter("flag");
	    	Integer flag = new Integer(ret);
	    	wechatFriendName = StringUtils.decode(wechatFriendName);
	    	String wechatFriendAlias = req.getParameter("wechatFriendAlias");
	    	wechatFriendAlias = StringUtils.decode(wechatFriendAlias);
	    	SaleWechatFriend friend = new SaleWechatFriend();
	    	System.err.println(wechatFriendName);
	    	friend.setCreateTime(System.currentTimeMillis());
	    	friend.setFriendRemark(wechatFriendAlias);
	    	friend.setFriendWechatId(wechatFriendId);
	    	friend.setUserId(UserId);
	    	friend.setFriendWechatName(wechatFriendName);
	    	friend.setWechatId(wechatId);
	    	friend.setFlag(flag);
	    	return saleWechatFriendRepository.save(friend);
		} catch (Exception e) {
			// TODO: handle exception
			return "";
		}
		
    }
	
	
//--------------------------------------------------------------用户注册   -------------------------------
    
	
	@RequestMapping(value="/user", method=RequestMethod.POST)
    @ResponseBody
    public Object addUser(HttpServletRequest req, HttpServletResponse resp)  {
    	try {
    		String userTel = req.getParameter("userTel");
    		User u = userRepository.findByPhone(userTel);
    		if (u.getId() != null) {
    			return Message.success(u.getId());
			}
    		String name = req.getParameter("name");
        	name = StringUtils.decode(name);
//    	    	String department = req.getParameter("department");
//    	    	String job = req.getParameter("job");
        	User user = new User();
        	user.setPhone(userTel);
        	user.setNickName(name);
        	user.setCreateTime(System.currentTimeMillis());
        	user = userRepository.save(user);
        	System.err.println("user = " + user.getId());
        	return Message.success(user.getId());
		} catch (Exception e) {
			// TODO: handle exception
			return Message.error();
		}
    }
    
	@RequestMapping(value="/user_id", method=RequestMethod.POST)
    @ResponseBody
    public Object getUserId(HttpServletRequest req, HttpServletResponse resp)  {
		String phone = req.getParameter("phone");
    	return userRepository.findByPhone(phone).getId();
    }
	
//---------------------------------------------------------------请求短信------------------------------
    
    @RequestMapping(value="/verfysms", method=RequestMethod.GET)
    @ResponseBody
    public Object verfySms(HttpServletRequest req, HttpServletResponse resp)  {
    	System.err.println("verfysms");
    	/*
    	 * 
    	 * 短信验证
    	 * 
    	 * */
    	return 123;
    }
    
    
 //-----------------------------------------------------------------机器-----------------------------   
    
    
    @RequestMapping(value="/machine", method=RequestMethod.POST)
    @ResponseBody
    public Object addMachine(HttpServletRequest req, HttpServletResponse resp)  {
    	try {
    		String name = req.getParameter("name");
        	name = StringUtils.decode(name);
        	String mobileTel = req.getParameter("mobileTel");
        	String wechatId = req.getParameter("wechatId");
        	String imei = req.getParameter("imei");
        	String userId = req.getParameter("userId");
        	SaleMachine saleMachine = new SaleMachine();
        	saleMachine.setCreateTime(System.currentTimeMillis());
        	saleMachine.setName(name);
        	saleMachine.setCreator(new Integer(userId));
        	saleMachine.setMeid(imei);
        	saleMachine.setUserId(new Integer(userId));
        	saleMachine.setPhoneNum(mobileTel);
        	saleMachine.setWechatId(wechatId);
        	System.err.println(saleMachine.toString());
        	return Message.success(saleMachineRepository.save(saleMachine));
		} catch (Exception e) {
			return Message.error();
		}
    }
    
    @RequestMapping(value="/get_id_by_machine", method=RequestMethod.POST)
    @ResponseBody
    public Object getUserIdByMachine(HttpServletRequest req, HttpServletResponse resp)  {
    	String imei = req.getParameter("imei");
    	return saleMachineRepository.findByMeid(imei).getUserId();
    }
    
    @RequestMapping(value="/get_machines", method=RequestMethod.POST)
    @ResponseBody
    public Object getMachines(HttpServletRequest req, HttpServletResponse resp)  {
    	String userId = req.getParameter("userId");
    	Integer UserId = new Integer(userId);
    	return saleMachineRepository.findByUserId(UserId);
    }
}
